package m.member.order.service;

import m.common.action.VueResult;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.member.manage.model.MemberInfo;
import m.member.manage.service.inter.MoneyRecordInter;
import m.member.order.model.MemberRecharge;
import m.member.order.model.OrderInfo;
import m.member.order.util.MoneyRecordUtil;
import m.system.db.TransactionManager;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.sql.SQLException;
import java.util.Date;

public class MemberRechargeService extends Service implements MoneyRecordInter {


    public String save(MemberRecharge model, AdminLogin admin, OrgGroupView dept) throws MException, SQLException {
        ModelCheckUtil.checkNotNull(model,"member.oid","serveAdmin.oid","type","payAmount","giveAmount","givePoint");
        model.setSurplusPayAmount(model.getPayAmount());
        model.setSurplusGiveAmount(model.getGiveAmount());
        String msg;
        if (ObjectUtil.isEmptyOid(model)) {
            model.setOid(GenerateID.generatePrimaryKey());
            model.setStatus("N");
            model.setInputAdmin(admin);
            model.setInputDept(dept);
            ModelUpdateUtil.insertModel(model);
            msg="保存成功";
        }else{
            ModelCheckUtil.equals(model,new String[]{"status","inputAdmin.oid"},new Object[]{"N",admin.getOid()},"只有本人录入且未确认的才能修改");
            ModelUpdateUtil.updateModel(model,"member.oid","serveAdmin.oid","type","payAmount","giveAmount","surplusPayAmount","surplusGiveAmount","givePoint","remark");
            msg="修改成功";
        }
        return msg;
    }
    public void confirm(MemberRecharge model, AdminLogin admin, OrgGroupView dept) throws Exception {
        if(ObjectUtil.isEmptyOid(model)) throw new MException(this.getClass(),"业务主键为空");
        ModelCheckUtil.equals(model,new String[]{"inputDept.oid"},new Object[]{dept.getOid()},"店铺不符，不能确认！");
        TransactionManager tm=new TransactionManager();
        try{
            tm.begin();
            model= ModelQueryList.getModel(model,"*");
            if(!"N".equals(model.getStatus())) throw new MException(this.getClass(),"只能确认未确认的记录。");
            model.setConfirmAdmin(admin);
            model.setConfirmDate(new Date());
            model.setStatus("Y");
            ModelUpdateUtil.updateModel(model,"confirmAdmin.oid","confirmDate","status");
            //添加金额记录
            MoneyRecordUtil.addRecharge(model);
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
    }
    public void setType(MemberRecharge model, AdminLogin admin, OrgGroupView dept) throws Exception {
        if(ObjectUtil.isEmptyOid(model)) throw new MException(this.getClass(),"业务主键为空");
        ModelCheckUtil.equals(model,new String[]{"inputDept.oid"},new Object[]{dept.getOid()},"店铺不符，不能确认！");
        ModelCheckUtil.checkNotNull(model,"type");
        TransactionManager tm=new TransactionManager();
        try{
            tm.begin();
            model.setConfirmAdmin(admin);
            model.setConfirmDate(new Date());
            ModelUpdateUtil.updateModel(model,"type","remark","confirmAdmin.oid","confirmDate");
            tm.commit();
        }catch (Exception e){
            tm.rollback();
            throw e;
        }
    }

    /**
     * 添加订单赊账
     */
    public void addCredit(OrderInfo order, Double payAmount) throws Exception {
        MemberRecharge model=new MemberRecharge();
        model.setOid(GenerateID.generatePrimaryKey());
        model.setOrder(order);
        model.setMember(order.getMember());
        model.setServeAdmin(order.getServeAdmin());
        model.setPayAmount(payAmount);
        model.setGiveAmount(0d);
        model.setGivePoint(0);
        model.setType("");
        model.setSurplusPayAmount(model.getPayAmount());
        model.setSurplusGiveAmount(model.getGiveAmount());
        model.setStatus("Y");
        model.setInputDate(order.getInputDate());
        model.setInputAdmin(order.getInputAdmin());
        model.setInputDept(order.getInputDept());
        ModelUpdateUtil.insertModel(model);
        //添加金额记录
        MoneyRecordUtil.addRecharge(model);
    }

    /**
     * 移除订单赊账
     */
    public void removeCredit(String orderOid) throws Exception {
        MemberRecharge model=ModelQueryList.getModel(MemberRecharge.class,new String[]{"*"}, QueryCondition.eq("order.oid",orderOid));
        if(null!=model&& StringUtil.isSpace(model.getType())){//付款类型未设置才能移除
            if(model.getSurplusPayAmount()<model.getPayAmount())
                throw new MException(this.getClass(),"赊账无法被退回");
            //移除金额记录
            MoneyRecordUtil.removeRecharge(model);
            ModelUpdateUtil.deleteModel(model);
        }
    }

    @Override
    public VueResult toViewPage(String busiOid) {
        return null;
    }
}
